package com.yoda.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yoda.youdianda.domain.YddAdvimg;
import com.yoda.youdianda.domain.YddAdvpos;
import com.yoda.youdianda.mapper.YddAdvimgMapper;
import com.yoda.youdianda.service.YddAdvimgService;
import com.yoda.youdianda.service.YddAdvposService;
import com.yoda.youdianda.util.PageCopyUtil;
import com.yoda.youdianda.vo.PageResult;
import com.yoda.youdianda.vo.YddAdvimgVo;
import com.yoda.youdianda.vo.YddAdvposVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class YddAdvimgServiceImpl extends ServiceImpl<YddAdvimgMapper, YddAdvimg> implements YddAdvimgService {
    @Autowired
    private YddAdvimgMapper advimgMapper;

    @Autowired
    private YddAdvposService yddAdvposService;

    @Override
    public Integer countAdvImg() {
        return advimgMapper.selectCount(
                new LambdaQueryWrapper<>());
    }

    @Override
    public PageResult<YddAdvimgVo> getImgPage(String advimgdesc, String advimgpos, Integer page, Integer limit) {
        //创建/设置条件
        LambdaQueryWrapper<YddAdvimg> imgWrapper = new LambdaQueryWrapper<>();
        imgWrapper.like(StringUtils.isNotEmpty(advimgdesc), YddAdvimg::getAdvimgdesc, advimgdesc);
        imgWrapper.eq(StringUtils.isNotEmpty(advimgpos), YddAdvimg::getAdvimgpos, advimgpos);
        //创建分页对象
        Page<YddAdvimg> imgPage = new Page<>(page, limit);
        advimgMapper.selectPage(imgPage, imgWrapper);

        //创建返回分页对象
        PageResult<YddAdvimgVo> imgPageResult = new PageResult<>();

        //page数值复制
        PageCopyUtil.pageCopy(imgPage, imgPageResult);

        //复制分页对象中的数据
        List<YddAdvimg> imgList = imgPage.getRecords();
        List<YddAdvimgVo> yddAdvimgVoList = new ArrayList<>();
        for (YddAdvimg yddAdvimg : imgList) {
            //创建Vo容器
            YddAdvimgVo yddAdvimgVo = new YddAdvimgVo();
            BeanUtils.copyProperties(yddAdvimg, yddAdvimgVo);
            YddAdvpos yddAdvpos = yddAdvposService.getById(yddAdvimg.getAdvimgpos());
            yddAdvimgVo.setAdvposname(yddAdvpos.getAdvposname());
            yddAdvimgVoList.add(yddAdvimgVo);
        }
        imgPageResult.setData(yddAdvimgVoList);
        return imgPageResult;
    }
}
